package com.xinqi.modules.sys.dict.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.sys.dict.convert.DictStageGradeConvert;
import com.xinqi.modules.sys.dict.domain.DictStageGradeEntity;
import com.xinqi.modules.sys.dict.dto.req.DictStageGradeCreateDTO;
import com.xinqi.modules.sys.dict.dto.req.DictStageGradeDeleteDTO;
import com.xinqi.modules.sys.dict.dto.req.DictStageGradeQueryDTO;
import com.xinqi.modules.sys.dict.dto.req.DictStageGradeUpdateDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictStageGradeResultDTO;
import com.xinqi.modules.sys.dict.manager.DictStageGradeManager;
import com.xinqi.modules.sys.dict.service.DictStageGradeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 字典-学段年级服务层实现
 *
 * @author: linkai
 * @date: 2023/05/03
 */
@Validated
@Service("dictStageGradeService")
@RequiredArgsConstructor
public class DictStageGradeServiceImpl implements DictStageGradeService {
    private final DictStageGradeConvert dictStageGradeConvert;
    private final DictStageGradeManager dictStageGradeManager;

    /**
     * 分页查询 字典-学段年级数据
     */
    @Override
    @DS("slaver")
    public PageInfo<DictStageGradeResultDTO> page(Param pageable, DictStageGradeQueryDTO query) {
        IPage<DictStageGradeEntity> page = dictStageGradeManager.page(Pages.page(pageable), wrapper(query));
        return Pages.convert(page, dictStageGradeConvert::convert);
    }

    /**
     * 查询 字典-学段年级数据
     */
    @Override
    public List<DictStageGradeResultDTO> find(DictStageGradeQueryDTO query) {
        return dictStageGradeManager.list(wrapper(query)).stream()
            .map(dictStageGradeConvert::convert).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<DictStageGradeEntity> wrapper(DictStageGradeQueryDTO query) {
        LambdaQueryWrapper<DictStageGradeEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(DictStageGradeEntity::getId, v));
        return wrapper;
    }

    /**
     * 根据id查询 字典-学段年级数据
     */
    @Override
    public DictStageGradeResultDTO findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return Optional.ofNullable(dictStageGradeManager.findById(id)).map(dictStageGradeConvert::convert).orElse(null);
    }

    /**
     * 新增 字典-学段年级数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(@Valid DictStageGradeCreateDTO dto) {
        DictStageGradeEntity entity = dictStageGradeConvert.create(dto);
        dictStageGradeManager.save(entity);
        return entity.getId();
    }

    /**
     * 根据id修改 字典-学段年级数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(@Valid DictStageGradeUpdateDTO dto) {
        Optional.ofNullable(dictStageGradeManager.getById(dto.getId()))
            .orElseThrow(() -> CommonErrors.BAD_REQUEST.asException("数据不存在"));
        DictStageGradeEntity entity = dictStageGradeConvert.update(dto);
        return dictStageGradeManager.updateById(entity);
    }

    /**
     * 根据id删除 字典-学段年级数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return dictStageGradeManager.removeById(id);
    }

    /**
     * 根据id删除 字典-学段年级数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(DictStageGradeDeleteDTO dto) {
        return deleteById(dto.getId());
    }

}
